*****************************************************************************
*** 			   ETR Distribution Plots		         ****
*****************************************************************************
local preyr 2013
local begyr 2016
local endyr 2019 

u "$cur_sample" if inlist(year,`preyr',`begyr',`endyr'), clear 
do $cleaning/renameSOI

global RATEVARS uscetr_soi

foreach v in $RATEVARS  {

	*Read data
	u "$cur_sample" if inlist(year,`preyr',`begyr',`endyr'), clear 
	do $cleaning/renameSOI
	
	*must observe ETR
	drop if pidom_soi<=0 & inlist("`v'", "uscetr_soi")

	*must observe in both years
	by firmid, sort: keep if _N > 1
	*ETR 
	g etr = `v'*100 

	*Change in ETR
	gegen t = group(year)
	tsset firmid t 
	g detr = d.etr

	//sum detr [pw=weightSOI], d

	*Winsorize to improve data visualizations
	winsor2 etr, cuts(0 95) replace //by(year)
	winsor2 detr, cuts(5 95) replace //by(year)

	*What share of firms got a tax increase, tax cut, no change?
	preserve
		gsort year weightSOI 
		bysort year: g N = _n 
		drop if mi(etr)
		gegen w = mean(weightSOI), by(N)
		keep year etr w N 
		reshape wide etr, i(N w) j(year)
		g diff = etr2019 - etr2016 
		g tinc = diff>0 
		g tcut = diff<0 
		g nochg = diff==0
		sum tinc tcut nochg [aw=w]
	restore

	*Create an integer weight, only to be used for histograms in percent, fraction, etc.	
	g weightSOI_int = floor(weightSOI*1000)
	*ETRs Level
	twoway  (hist etr if t==2 [fw=weightSOI_int], color(gs0%40) percent width(1)) ///
		(hist etr if t==3 [fw=weightSOI_int], color(gs10%40) percent width(1) lcolor(gs1%70)), ///
		legend(order(1 "`begyr'" 2 "`endyr'") pos(12) row(1) size(medlarge)) /*title("US Cash ETR in `begyr' and `endyr'", size(large)) */ ///
		xtitle("ETR (Percent)", size(medlarge)) ytitle("Percent of Firms", size(medlarge)) /// 
		xlab(, labsize(medium) format(%9.0f)) ylab(,labsize(medium))
		graph export "$results/`v'_`begyr'_`endyr'.pdf", replace
		
	*ETR Levels Public MNE
	twoway  (hist etr if t==2  & firmtype_alt==1 [fw=weightSOI_int], color(gs0%40) percent width(1)) ///
		(hist etr if t==3  & firmtype_alt==1 [fw=weightSOI_int], color(gs10%40) percent width(1) lcolor(gs1%70)), ///
		legend(order(1 "`begyr'" 2 "`endyr'") pos(12) row(1) size(medlarge)) /*title("US Cash ETR in `begyr' and `endyr'", size(large)) */ ///
		xtitle("ETR (Percent)", size(medlarge)) ytitle("Percent of Firms", size(medlarge)) /// 
		xlab(, labsize(medium) format(%9.0f)) ylab(,labsize(medium))
		graph export "$results/`v'_`begyr'_`endyr'_pubmne.pdf", replace

	*ETR Levels Public domestic
	twoway  (hist etr if t==2  & firmtype_alt==2 [fw=weightSOI_int], color(gs0%40) percent width(1)) ///
		(hist etr if t==3  & firmtype_alt==2 [fw=weightSOI_int], color(gs10%40) percent width(1) lcolor(gs1%70)), ///
		legend(order(1 "`begyr'" 2 "`endyr'") pos(12) row(1) size(medlarge)) /*title("US Cash ETR in `begyr' and `endyr'", size(large)) */ ///
		xtitle("ETR (Percent)", size(medlarge)) ytitle("Percent of Firms", size(medlarge)) /// 
		xlab(, labsize(medium) format(%9.0f)) ylab(,labsize(medium))
		graph export "$results/`v'_`begyr'_`endyr'_pubdom.pdf", replace
	*ETR Levels Priv MNE
	twoway  (hist etr if t==2  & firmtype_alt==3 [fw=weightSOI_int], color(gs0%40) percent width(1)) ///
		(hist etr if t==3  & firmtype_alt==3 [fw=weightSOI_int], color(gs10%40) percent width(1) lcolor(gs1%70)), ///
		legend(order(1 "`begyr'" 2 "`endyr'") pos(12) row(1) size(medlarge)) /*title("US Cash ETR in `begyr' and `endyr'", size(large)) */ ///
		xtitle("ETR (Percent)", size(medlarge)) ytitle("Percent of Firms", size(medlarge)) /// 
		xlab(, labsize(medium) format(%9.0f)) ylab(,labsize(medium))
		graph export "$results/`v'_`begyr'_`endyr'_privmne.pdf", replace
		
	*ETR Levels Large Priv domestic
	twoway  (hist etr if t==2  & firmtype_alt==4 [fw=weightSOI_int], color(gs0%40) percent width(1)) ///
		(hist etr if t==3  & firmtype_alt==4 [fw=weightSOI_int], color(gs10%40) percent width(1) lcolor(gs1%70)), ///
		legend(order(1 "`begyr'" 2 "`endyr'") pos(12) row(1) size(medlarge)) /*title("US Cash ETR in `begyr' and `endyr'", size(large)) */ ///
		xtitle("ETR (Percent)", size(medlarge)) ytitle("Percent of Firms", size(medlarge)) /// 
		xlab(, labsize(medium) format(%9.0f)) ylab(,labsize(medium))
		graph export "$results/`v'_`begyr'_`endyr'_lprivdom.pdf", replace	
		
	*ETR Levels Small Priv domestic
	twoway  (hist etr if t==2  & firmtype_alt==5 [fw=weightSOI_int], color(gs0%40) percent width(1)) ///
		(hist etr if t==3  & firmtype_alt==5 [fw=weightSOI_int], color(gs10%40) percent width(1) lcolor(gs1%70)), ///
		legend(order(1 "`begyr'" 2 "`endyr'") pos(12) row(1) size(medlarge)) /*title("US Cash ETR in `begyr' and `endyr'", size(large)) */ ///
		xtitle("ETR (Percent)", size(medlarge)) ytitle("Percent of Firms", size(medlarge)) /// 
		xlab(, labsize(medium) format(%9.0f)) ylab(,labsize(medium))
		graph export "$results/`v'_`begyr'_`endyr'_sprivdom.pdf", replace	
}
